﻿// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{
    public partial class RoutesManagementPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
        }
        public string BindPlanes(object objId)
        {
            long Id = Convert.ToInt64(objId.ToString());
            Planes plane = new Planes(Id);
            return plane.PlaneId;
        }
        public string BindCities(object objId)
        {
            long Id = Convert.ToInt64(objId.ToString());
            AirPorts air = new AirPorts(Id);
            Cities city = new Cities(air.CityId);
            air.Name = city.Name + " (" + air.AirPortId + ")";
            return air.Name;
        }
        private void BindRoutesSearch()
        {
            try
            {
                bool delete = cbDelete.Checked;
                string text = txtSearchText.Text.Trim().ToString();
                grvRoutes.DataSource = Routes.GetSearch(text, delete);
                grvRoutes.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            //BindRoutesSearch();
            bindRoute();
        }

        private void bindRoute()
        {
            grvRoutes.DataSource = Routes.GetAll();
            grvRoutes.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindRoutesSearch();
        }
        protected void grvRoutes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvRoutes.PageIndex = e.NewPageIndex;
            grvRoutes.DataBind();
            BindRoutesSearch();
        }

        protected void grvRoutes_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = int.Parse(grvRoutes.DataKeys[e.NewEditIndex].Value.ToString());
            Response.Redirect("AddRoute.aspx?Id=" + id);
        }

        protected void grvRoutes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(grvRoutes.DataKeys[e.RowIndex].Value.ToString());
            Routes route = new Routes(id);
            route.IsDeleted = true;
            route.Save();
            BindRoutesSearch();
        }
        protected void grvRoutes_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = int.Parse(grvRoutes.DataKeys[e.RowIndex].Value.ToString());
            Routes route = new Routes(id);
            route.IsDeleted = false;
            route.Save();
            BindRoutesSearch();
        }
    }
}